
//////////////////////////////////////////////////////////////////////////////////
/// Thread recuperant un message reseau et effectuant l'action correspondante  ///
//////////////////////////////////////////////////////////////////////////////////

#include "include.h"

void* waitconnection(void* ptr)
{
	sthread a = *((sthread*) ptr);
    int sock = a.sock;
	terrain *j2 = a.t;
	
	stringw *txt = a.txt;
	
    char bufferRecv[99];
	int num;
	int n;
	
	
    while (1)
    {
		
		for (int i=0;i<99;i++)
			bufferRecv[i] = 0;
        int len;
        if(len = recv(sock, bufferRecv, 99, 0) == -1)
		{
            printf("Erreur de recv\n");
			exit(-1);
		}
		int code;
		char str[99];
		char temp[99];
		code = bufferRecv[0];
		for (int i=0;i<98;i++)
			temp[i] = bufferRecv[i+2];
		switch (code)
		{
		case 1:
			piocher(j2);
			sprintf(str,"%s",temp);
		break;
			
		case 2:
			detruire(j2,0,0);
			sprintf(str,"%s",temp);
		break;
			
		case 3:
			melanger(j2);
			sprintf(str,"%s",temp);
		break;

		case 4:
			sscanf(temp,"%i %s",&num,str);
			invoquer(j2,num);
		break;
			
		case 5:
			sscanf(temp,"%i %s",&num,str);
			n = invoquer(j2,num);
			pivoter(j2,n);
			retourner(j2,0,n);
		break;
			
		case 6:
			sscanf(temp,"%i %s",&num,str);
			activer(j2,num);
		break;

		case 7:
			sscanf(temp,"%i %s",&num,str);
			n = activer(j2,num);
			retourner(j2,1,n);
		break;
			
		case 8:
			sscanf(temp,"%i %s",&num,str);
			melanger2(j2,0,num);
		break;
			
		case 9:
			sscanf(temp,"%i %s",&num,str);
			detruire(j2,1,num);
		break;
			
		case 10:
			sscanf(temp,"%i %s",&num,str);
			retourner(j2,0,num);
		break;
			
		case 11:
			sscanf(temp,"%i %s",&num,str);
			pivoter(j2,num);
		break;
			
		case 12:
			sscanf(temp,"%i %s",&num,str);
			melanger2(j2,1,num);
		break;
		
		case 13:
			sscanf(temp,"%i %s",&num,str);
			detruire(j2,2,num);
		break;
			
		case 14:
			sscanf(temp,"%i %s",&num,str);
			retourner(j2,1,num);
		break;
			
		case 15:
			sscanf(temp,"%i %s",&num,str);
			melanger2(j2,2,num);
		break;
		
		case 16:
			sscanf(temp,"%i %s",&num,str);
			detruire(j2,3,num);
			
		break;
		
		default:
			sprintf(str,"Adversaire dit : %s ",temp);
		break;
		}
	    *txt += str;
	    *txt += "\n";
    }
}        

